package com.gewu.bookeeping.View.ItemFragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gewu.bookeeping.MyDb.bean.ALIBeanIn;
import com.gewu.bookeeping.MyDb.bean.ALiBeanOut;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.MyDb.dao.ALiDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.ALiDaoOutPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoOutPerson;
import com.gewu.bookeeping.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ForOneItem2Fargemnt extends Fragment {

    private LineChart lineChart;
    private TextView tv;
    private int index;
    private int type;
    private float nextInt;
    private List<WeChtBeanOut> quer1;
    private List<WeChtBeanIn> quer2;
    private List<ALiBeanOut> quer3;
    private List<ALIBeanIn> quer4;
    private List<Entry> entries = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_for_one_item2_fargemnt, container, false);
        initview(inflate);
        if (quer1 != null) {
            setDaet1();
        }
        if (quer2 != null) {
            setDaet1();
        }
        if (quer3 != null) {
            setDaet1();
        }
        if (quer4 != null) {
            setDaet1();
        }
        return inflate;
    }

    private void initview(View inflate) {
        lineChart = inflate.findViewById(R.id.LineChart);
        tv = inflate.findViewById(R.id.tv);

        switch (type) {
            case 0:
                if (index == 0) {
                    WeChtDaoOutPerson weChtDaoOutPerson = new WeChtDaoOutPerson(getContext());
                    quer1 = weChtDaoOutPerson.quer();
                } else {
                    WeChtDaoInPerson weChtDaoInPerson = new WeChtDaoInPerson(getContext());
                    quer2 = weChtDaoInPerson.quer();
                }
                break;
            case 1:
                if (index == 0) {
                    ALiDaoOutPerson aLiDaoOutPerson = new ALiDaoOutPerson(getContext());
                    quer3 = aLiDaoOutPerson.quer();
                } else {
                    ALiDaoInPerson aLiDaoInPerson = new ALiDaoInPerson(getContext());
                    quer4 = aLiDaoInPerson.quer();
                }
                break;
        }
    }

    /**
     * 类型 收入还是支出 微信/支付宝/短信
     *
     * @param index
     * @param flage
     */
    public void add(int index, int flage) {
        this.index = index;
        this.type = flage;
    }


    private void setDaet1() {
        for (int i = 0; i < 10; i++) {
            nextInt = new Random().nextInt(100);
            entries.add(new Entry(i, nextInt));
        }
        setDaet();
    }

    private void setDaet() {
        LineDataSet dataSet = new LineDataSet(entries, "数据"); // add entries to dataset
        dataSet.setColor(Color.parseColor("#AABCC6"));//线条颜色
        dataSet.setCircleColor(Color.parseColor("#7d7d7d"));//圆点颜色
        dataSet.setDrawValues(false);                     // 设置是否显示数据点的值
        dataSet.setDrawCircleHole(false);                 // 设置数据点是空心还是实心，默认空心
        dataSet.setCircleColor(Color.parseColor("#AABCC6"));              // 设置数据点的颜色
        dataSet.setCircleSize(1);                         // 设置数据点的大小
        dataSet.setHighLightColor(Color.parseColor("#AABCC6"));            // 设置点击时高亮的点的颜色
        dataSet.setLineWidth(2f);//线条宽度
        //设置样式
        YAxis rightAxis = lineChart.getAxisRight();
        //设置图表右边的y轴禁用
        rightAxis.setEnabled(false);
        YAxis leftAxis = lineChart.getAxisLeft();
        //设置图表左边的y轴禁用
        leftAxis.setEnabled(true);
        leftAxis.setTextColor(Color.parseColor("#333333"));
        //设置x轴
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setTextColor(Color.parseColor("#333333"));
        xAxis.setTextSize(11f);
        xAxis.setAxisMinimum(0f);
        xAxis.setDrawAxisLine(false);//是否绘制轴线
        xAxis.setDrawGridLines(false);//设置x轴上每个点对应的线
        xAxis.setDrawLabels(true);//绘制标签  指x轴上的对应数值
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置x轴的显示位置
        xAxis.setGranularity(1f);//禁止放大后x轴标签重绘

        //透明化图例
        Legend legend = lineChart.getLegend();
        legend.setForm(Legend.LegendForm.NONE);
        legend.setTextColor(Color.WHITE);

        //隐藏x轴描述
        Description description = new Description();
        description.setEnabled(false);
        lineChart.setDescription(description);

        LineData lineData = new LineData(dataSet);
        lineChart.setData(lineData);
        lineChart.invalidate(); // refresh


        if (nextInt > 5) {
            tv.setText("您的消费记录良好,继续保持");
        } else if (nextInt > 40) {
            tv.setText("您的消费记录真的是太棒啦，继续保持努力争做月光族");
        } else if (nextInt > 80) {
            tv.setText("您的消费记录良好,继续保持，继续保持再过一个月你就可以秦家荡产啦");
        } else if (nextInt == 0) {
            tv.setText("您的消费记录良好,您没有消费记录");
        } else {
            tv.setText("您的消费记录良好,继续保持");
        }
    }
}